package main

import (
	"strconv"

	"github.com/spf13/viper"
)

type Config struct {
	DBMode     string
	DBHost     string
	DBPort     string
	DBUser     string
	DBPassword string
	DBDatabase string
	DBCharset  string
	SQLitePath string

	Port   string
	Mode   string
	Domain string
	PCQ    float64
}

func MakeDefaultConfig() Config {
	return Config{
		DBMode:     "sqlite",
		DBHost:     "localhost",
		DBPort:     "3306",
		DBUser:     "root",
		DBPassword: "test",
		DBDatabase: "root",
		DBCharset:  "utf8mb4",
		SQLitePath: "./goview.db",
		Port:       "8088",
		Mode:       "debug",
		Domain:     "http://localhost:8088",
		PCQ:        50.0,
	}
}

func ReadConfig() *Config {
	// 读取配置项
	port := viper.GetString("server.port")
	mode := viper.GetString("server.mode")
	domain := viper.GetString("server.domain")
	pcq := viper.GetString("server.pcq")
	db_mode := viper.GetString("server.db_mode")

	db_host := viper.GetString("server.host")
	db_port := viper.GetString("server.port")
	db_user := viper.GetString("server.user")
	db_password := viper.GetString("server.password")
	db_database := viper.GetString("server.database")
	db_charset := viper.GetString("server.charset")
	sqlite_pass := viper.GetString("server.sqlite_path")

	config := MakeDefaultConfig()
	if db_mode != "" {
		config.DBMode = db_mode
	}
	if db_host != "" {
		config.DBHost = db_host
	}
	if db_port != "" {
		config.DBPort = db_port
	}
	if db_user != "" {
		config.DBUser = db_user
	}
	if db_password != "" {
		config.DBPassword = db_password
	}
	if db_database != "" {
		config.DBDatabase = db_database
	}
	if db_charset != "" {
		config.DBCharset = db_charset
	}
	if sqlite_pass != "" {
		config.SQLitePath = sqlite_pass
	}
	if port != "" {
		config.Port = port
	}
	if mode != "" {
		config.Mode = mode
	}
	if domain != "" {
		config.Domain = domain
	}
	if pcq != "" {
		pcq, _ := strconv.ParseFloat(pcq, 32)
		config.PCQ = pcq
	}
	return &config

}
